home *** CD-ROM | disk | FTP | other *** search
/ SPACE 1 / SPACE - Library 1 - Volume 1.iso / program / 363 / tprolog1 / tictac.toe < prev    next >
Text File  |  1990-02-03  |  4KB  |  105 lines

  1.  
  2. tictactoe :- grf_mode, grf_mse_show(0),
  3.       tag(tictactoe(human)), grf_mse_hide, txt_mode.
  4.  
  5. tictactoe(human) :- screen, play(human, [u, u, u, u, u, u, u, u, u]),
  6.       tictactoe(computer).
  7. tictactoe(computer) :- screen, play(computer, [u, u, u, u, u, u, u, u, u]),
  8.       tictactoe(human).
  9.  
  10. play(_, Board) :- wins(o, Board), delay(500).
  11. play(_, Board) :- wins(x, Board), delay(500).
  12. play(_, Board) :- not member(u, Board), delay(500).
  13.  
  14. play(human, Board) :- repeat, get_move(Pos), legal(Pos, Board), !,
  15.       move(o, 0, Pos, Board, NewBoard), play(computer, NewBoard).
  16.  
  17. play(computer, Board) :- think(Board, Pos),
  18.       move(x, 0, Pos, Board, NewBoard), play(human, NewBoard).
  19.  
  20. move(Sym, N, N, [u | R], [Sym | R]) :- show(N, Sym), !.
  21. move(Sym, N, L, [H | T], [H | NT]) :- sum(N, 1, N1), move(Sym, N1, L, T, NT).
  22.  
  23. legal(0, [u | _]) :- !.
  24. legal(N, [_ | T]) :- sum(N1, 1, N), legal(N1, T).
  25.  
  26. get_move(Pos) :- repeat, request(X, Y), stop_button(X, Y),
  27.       less(170, X), less(X, 470), less(50, Y), less(Y, 350),
  28.       Pos is 3 * ((Y - 50) / 100) + (X - 170) / 100, !.
  29.  
  30. request(X, Y) :- repeat, grf_mse_state(0, _, _), !,
  31.       repeat, grf_mse_state(1, X, Y), !.
  32.  
  33. stop_button(X, Y) :- less(30, X), less(X, 70), less(30, Y), less(Y, 70),
  34.       tagexit(tictactoe(_)).
  35. stop_button(_, _).
  36.  
  37. % the computer's strategy :
  38.  
  39. % try to use a winning situation
  40.  
  41. think(Board, Pos) :- insert(x, Board, Pos, NewBoard), wins(x, NewBoard).
  42.  
  43. % try to destroy the human's winning situation
  44.  
  45. think(Board, Pos) :- insert(o, Board, Pos, NewBoard), wins(o, NewBoard).
  46.  
  47. % select an empty field, but prefer center to corners to edges
  48.  
  49. think([_, _, _, _, u, _, _, _, _], 4).
  50. think([u, _, _, _, _, _, _, _, _], 0).
  51. think([_, _, u, _, _, _, _, _, _], 2).
  52. think([_, _, _, _, _, _, u, _, _], 6).
  53. think([_, _, _, _, _, _, _, _, u], 8).
  54. think([_, u, _, _, _, _, _, _, _], 1).
  55. think([_, _, _, u, _, _, _, _, _], 3).
  56. think([_, _, _, _, _, u, _, _, _], 5).
  57. think([_, _, _, _, _, _, _, u, _], 7).
  58.  
  59. insert(Sym, [u | R], 0, [Sym | R]).
  60. insert(Sym, [H | T], N, [H | NT]) :- insert(Sym, T, N1, NT), sum(N1, 1, N).
  61.  
  62. % determining the end of a game :
  63.  
  64. wins(X, [X, X, X, _, _, _, _, _, _]).
  65. wins(X, [_, _, _, X, X, X, _, _, _]).
  66. wins(X, [_, _, _, _, _, _, X, X, X]).
  67. wins(X, [X, _, _, X, _, _, X, _, _]).
  68. wins(X, [_, X, _, _, X, _, _, X, _]).
  69. wins(X, [_, _, X, _, _, X, _, _, X]).
  70. wins(X, [X, _, _, _, X, _, _, _, X]).
  71. wins(X, [_, _, X, _, X, _, X, _, _]).
  72.  
  73. delay(0).
  74. delay(N) :- sum(N1, 1, N), bell, delay(N1).
  75.  
  76. % graphics :
  77.  
  78. screen :- grf_mse_hide, grf_f_type(2), grf_f_style(4),
  79.       grf_box(0, 0, 639, 399), grf_f_type(0), grf_rfbox(150, 30, 489, 369),
  80.       clr(50), clr(150), clr(250),
  81.       grf_f_type(2), grf_f_style(1), grf_rfbox(30, 30, 70, 70),
  82.       grf_t_effects(16),
  83.       grf_text(34, 42, 'STOP'), grf_mse_show(0).
  84.  
  85. clr(Y) :- square(170, Y), square(270, Y), square(370, Y).
  86.  
  87. show(Number, Symbol) :- prod(3, Div, Mod, Number), X is 100*Mod + 170,
  88.       Y is 100*Div + 50, grf_mse_hide, show(X, Y, Symbol), grf_mse_show(0).
  89.  
  90. show(X, Y, o) :- !, circle(X, Y).
  91. show(X, Y, x) :- cross(X, Y).
  92.  
  93. square(X, Y) :- sum(X, 99, X1), sum(Y, 99, Y1),
  94.       grf_f_type(0), grf_bar(X, Y, X1, Y1).
  95.  
  96. circle(X, Y) :- sum(X, 50, X1), sum(Y, 50, Y1), grf_l_width(15),
  97.       grf_arc(X1, Y1, 30, 0, 3600).
  98.  
  99. cross(X, Y) :- sum(X, 20, X1), sum(Y, 20, Y1), sum(X, 80, X2), sum(Y, 80, Y2),
  100.       grf_l_width(15), grf_l_ends(2, 2),
  101.       grf_pline([X1, Y1, X2, Y2]), grf_pline([X1, Y2, X2, Y1]).
  102.  
  103. end.
  104.  
  105.